// pages/bj_order/index.js
import {getOrderList,getOrdersDriver} from '../../api/user.js'
import {orderCancel,postOrderPay} from '../../api/order.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '0',
            'title':'我的订单'
        },
        navH:'',
        type:[
            {name:'全部',id:0},
            {name:'已预约',id:1},
            {name:'进行中',id:2},
            {name:'已完成',id:3},
            {name:'退款订单',id:4}
        ],
        type_id:0,

        list:[],
        page:1,
        loading:true,

        isshow:false,
        siji_data:'',//司机信息
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
         this.setData({
             navH: getApp().globalData.navHeight
          });
          
    },
    
    //切换
    qiehuan(e){
        let id = e.currentTarget.dataset.id;
        let that =this;
        that.setData({
             type_id:id,
             page:1,
             loading:true
        },function(){
            wx.showLoading({
              title: ' ',
            })
            that.getlist()
        })
    },
    getlist(){ 
        let data = {
            page:this.data.page,
            type:this.data.type_id,
            limit:10
        }
         getOrderList(data).then(res=>{
             if(this.data.page==1){
                  this.setData({
                       list:res.data,
                       loading:res.data.length>=10
                  })
             }else{
                 this.setData({
                     list:this.data.list.concat(res.data),
                     loading:res.data.length>=10
                 })
             }
             wx.hideLoading()
         }).catch(e=>{})
    },

    //复制
    fuzhi(e){
          let id = e.currentTarget.dataset.id;
           wx.setClipboardData({
                data: id,
                success: function (res) {
                    getApp().Tips({title:'复制成功'});
                }
            })
    },

    //修改
    edit_order(e){
        let id = e.currentTarget.dataset.id
         wx.navigateTo({
           url: './edit?id='+id,
         })
    },
    //修改价格
    edit_price(e){
        let id = e.currentTarget.dataset.id
         wx.navigateTo({
           url: '/pages/siji_order/edit?id='+id,
         })
    },
    //详情
    go_xq(e){
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: './details?id='+id,
        })   
    },

    //取消ID
    close_order(e){
         let id = e.currentTarget.dataset.id;
         let index = e.currentTarget.dataset.index;
         let that =this;
         wx.showModal({
           title: '大象搬家',
           content: '是否确认取消该订单？',
           complete: (res) => { 
                if (res.confirm) {
                    wx.showLoading({
                      title: '正在取消订单',
                    })
                    orderCancel(id).then(res=>{
                           wx.hideLoading()
                           getApp().Tips({title:res.msg}) 
                           this.data.list.splice(index,1)
                           that.setData({
                               list:this.data.list
                           })
                    }).catch(e=>{
                         wx.hideLoading()
                         getApp().Tips({title:e}) 
                    })
                }
           }
         })
    },

    //支付
    pay_order(e){
        let id = e.currentTarget.dataset.id;
        let data ={
            from:'weixin',
            id:id
        }
        wx.showLoading({
          title: '支付中',
        })
       let that = this;
        postOrderPay(data).then(res=>{
            let jsConfig = res.data.result.jsConfig
            wx.requestPayment({
              timeStamp: jsConfig.timestamp,
              nonceStr: jsConfig.nonceStr,
              package: jsConfig.package,
              signType: jsConfig.signType,
              paySign: jsConfig.paySign,
              success: function (res) {
                    wx.hideLoading();
                    getApp().Tips({title:'支付成功'});
                    that.setData({
                        page:1,
                        loading:true
                    },function(){
                        that.getlist()
                    })
              },
              fail: function (e) {
                    wx.hideLoading();
                    return getApp().Tips({ title: '取消支付' });
              },
              complete: function (e) {
                    wx.hideLoading(); 
                    if (res.errMsg == 'requestPayment:cancel') return getApp().Tips({ title: '取消支付' });
              },
            })
        }).catch(e=>{
            wx.hideLoading()
            getApp().Tips({title:e});
        })
    },

    close_pop(){
        this.setData({isshow:false})
    },
    //查看司机信息
    look_sj(e){
        wx.showLoading({
          title: '',
        })
         let id =e.currentTarget.dataset.id;
         getOrdersDriver({oid:id}).then(res=>{
            wx.hideLoading()
            let that =this;  
            that.setData({
                   siji_data:res.data
              },function(){
                 that.setData({isshow:true})
              })
         }).catch(e=>{
             wx.hideLoading()
             getApp().Tips({title:e})
         })
    },

    //退款
    order_return(e){
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/pages/goods_return/index?orderId='+id,
        }) 
    },

    //打电话
    ddh(e){
         let dh = e.currentTarget.dataset.dh;
         wx.makePhoneCall({
           phoneNumber: dh,
         })
    },
    

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
         let that = this;
         if( getApp().globalData.isLog==false){
             getApp().Tips({title:'请登录'})
         } 
         that.setData({
             page:1,
             loading:true
         },function(){
            that.getlist()
         })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
         let that = this;
          if(that.data.loading){
               that.setData({
                   loading:false,
                   page:that.data.page+1
               },function(){
                   that.getlist()
               })
          }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})